'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, TEXT, JSON, DECIMAL } = app.Sequelize;

  const WearRecord = app.model.define('wear_record', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    outfit_id: {
      type: INTEGER,
      allowNull: true,
      comment: '搭配ID',
    },
    wear_date: {
      type: DATE,
      allowNull: false,
      comment: '穿着日期',
    },
    occasion: {
      type: STRING(100),
      allowNull: true,
      comment: '场合',
    },
    location: {
      type: STRING(100),
      allowNull: true,
      comment: '地点',
    },
    weather: {
      type: JSON,
      allowNull: true,
      comment: '天气信息',
    },
    temperature: {
      type: DECIMAL(4, 1),
      allowNull: true,
      comment: '温度',
    },
    mood: {
      type: STRING(50),
      allowNull: true,
      comment: '心情',
    },
    rating: {
      type: DECIMAL(2, 1),
      allowNull: true,
      comment: '评分(1-5)',
    },
    photos: {
      type: JSON,
      allowNull: true,
      comment: '照片',
    },
    notes: {
      type: TEXT,
      allowNull: true,
      comment: '备注',
    },
    tags: {
      type: JSON,
      allowNull: true,
      comment: '标签',
    },
    // 时间戳字段由Sequelize自动管理，根据config.sequelize.define配置
    // created_at, updated_at, deleted_at 会自动添加
  }, {
    tableName: 'wear_records',
    comment: '穿搭记录表',
    paranoid: true,  // 明确启用软删除
    indexes: [
      {
        fields: ['user_id'],
      },
      {
        fields: ['outfit_id'],
      },
      {
        fields: ['wear_date'],
      },
      {
        fields: ['occasion'],
      },
    ],
  });

  WearRecord.associate = function() {
    // 穿搭记录属于用户
    app.model.WearRecord.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });

    // 穿搭记录属于搭配
    app.model.WearRecord.belongsTo(app.model.Outfit, {
      foreignKey: 'outfit_id',
      as: 'outfit',
    });
  };

  return WearRecord;
};
